<!DOCTYPE html>
<!-- Tutorial series by David Rousset: http://blogs.msdn.com/davrous -->
<!-- Based on this EaselJS sample: http://easeljs.com/examples/bitmapAnimation.html -->
<html>

<head>
    <title>boxBamGameTest</title>

    <!-- Import EaselJS Framework -->
    <script src="js/jquery-2.1.1.min.js"></script>
    <script src="js/easeljs-0.6.0.min.js"></script>
    <script src="js/gamefwk/util/ImgLoader.js"></script>
    <script src="js/gamefwk/Vector2.js"></script>
    <script src="js/gamefwk/Framework.js"></script>
    <script src="js/gamefwk/Rectangle.js"></script>
    <script src="js/gamefwk/Color.js"></script>

    <script src="js/gamefwk/go/GameObject.js"></script>
    <script src="js/gamefwk/go/char/Mario.js"></script>
    <script src="js/gamefwk/go/char/Bomberman.js"></script>
    <script src="js/gamefwk/go/char/Red.js"></script>
    <script src="js/gamefwk/go/char/Brick.js"></script>

    <script src="js/gamefwk/go/cont/Controller.js"></script>
    <script src="js/gamefwk/go/cont/SimpleController.js"></script>
    <script src="js/gamefwk/go/cont/SimpleController4Axis.js"></script>

    <script src="js/Box2dWeb-2.1.a.3.min.js"></script>
    <script src="js/gamefwk/go/upd/Updater.js"></script>
    <script src="js/gamefwk/go/upd/PosUpdater.js"></script>
    <script src="js/gamefwk/go/upd/SimpleRpgCollisionUpdater.js"></script>

    <script src="js/gamefwk/go/gen/Gen.js"></script>
    <script src="js/gamefwk/go/gen/BrickGen.js"></script>
    <script src="js/gamefwk/map/MapGen.js"></script>

    <script src="js/goog/base.js"></script>
    <script src="js/goog/math/rect.js"></script>
</head>

<body id="body">

    <canvas id="testCanvas" width="800" height="600" style="background-color:#607559">
        Your browser doesn't support canvas. Please download IE9+ on <a href="http://ie.microsoft.com/testdrive">IE Test Drive</a>
    </canvas>


    <script>
    GameSys = {};

    GameSys.canvas = document.getElementById("testCanvas");
    GameSys.ctx = GameSys.canvas.getContext("2d");

    GameSys.fps = 60;
    GameSys.imagesToLoad = [{
        path: "img/mario.png",
        id: "mario"
    }, {
        path: "img/brick.png",
        id: "brick"
    }, {
        path: "img/BombermanDSWalk.png",
        id: "bombermanWalk"
    }, {
        path: "img/red.png",
        id: "redWalk"
    }];
    GameSys.imgLoader = new ImgLoader(GameSys.imagesToLoad.length, startGame);
    GameSys.gameObjects = [];

    GameSys.drawScale = 10;
    GameSys.debugModeOn = false;
    GameSys.putMario = false;

    GameSys.screenScale = 0.9;
    GameSys.width = getWidth() * GameSys.screenScale;
    GameSys.height = getHeight() * GameSys.screenScale;

    $(document).ready(init);


    function loadImages() {
            for (var i = 0; i < GameSys.imagesToLoad.length; i++) {
                GameSys.imgLoader.load(GameSys.imagesToLoad[i].path, GameSys.imagesToLoad[i].id);
            }
        } //loadImages

    function initWorld() {
            // create a new stage and point it at our canvas:
            GameSys.stage = new createjs.Stage(GameSys.canvas);
            GameSys.gameContainer = new createjs.Container();
            GameSys.stage.addChild(GameSys.gameContainer);
        } //initWorld

    function initGameObjects() {
            var scale = {
                x: 2,
                y: 2
            };
            var hero = new Red(GameSys.width / 2, GameSys.height / 2);
            GameSys.hero = hero;
            GameSys.gameObjects.push(hero);
            var controller4Axis = new SimpleController4Axis(GameSys.hero);
            var onDebug = function() {
                if (input.space) {
                    console.log(getBounds(this));
                }
                if (input.i) {
                    var imgr = this.animation.spriteSheet._frames[this.animation.currentFrame];
                    console.log(imgr);
                }
                if (input.h) {
                    console.log(this);
                }
            }
            var onDebugg = onDebug.bind(hero);
            controller4Axis.onDebug = onDebugg;
            hero.addController(controller4Axis);

            scale = {
                x: 0.2,
                y: 0.2
            };
            GameSys.bricks = [];
            var bg = new BrickGen(374, 374, 187, 187, scale.x, scale.y, "brick");

            var brick = bg.run(300, 200);
            GameSys.gameObjects.push(brick);
            GameSys.bricks.push(brick);


            var brick = bg.run(500, 300);
            GameSys.gameObjects.push(brick);
            GameSys.bricks.push(brick);

            var brick = bg.run(900, 300);
            GameSys.gameObjects.push(brick);
            GameSys.bricks.push(brick);

            var brick = bg.run(800, 150);
            GameSys.gameObjects.push(brick);
            GameSys.bricks.push(brick);

            var brick = bg.run(250, 100);
            GameSys.gameObjects.push(brick);
            GameSys.bricks.push(brick);

            var brick = bg.run(1200, 250);
            GameSys.gameObjects.push(brick);
            GameSys.bricks.push(brick);

            var brick = bg.run(250, 400);
            GameSys.gameObjects.push(brick);
            GameSys.bricks.push(brick);

            var brick = bg.run(450, 600);
            GameSys.gameObjects.push(brick);
            GameSys.bricks.push(brick);

            var brick = bg.run(1500, 600);
            GameSys.gameObjects.push(brick);
            GameSys.bricks.push(brick);


            var updater = new PosUpdater(GameSys.hero, GameSys.bricks);
            GameSys.hero.addUpdater(updater);


            turnOnDebugDraw();
        } //initGameObjects

    function turnOnDebugDraw() {
        var go = GameSys.gameObjects;
        for (var i = 0; i < go.length; i++) {
            go[i].debugDrawOn = true;
        }
    }

    function init() {
            setCanvasSize(GameSys.width, GameSys.height);

            initWorld();

            loadImages();

            initGameObjects();
        } //init

    function loadAnimations() {
            for (var i = 0; i < GameSys.gameObjects.length; i++)
                GameSys.gameObjects[i].loadAnimations();
        } //loadAnimations

    function startGame() {
            loadAnimations();

            for (var i = 0; i < GameSys.gameObjects.length; i++)
                GameSys.gameContainer.addChild(GameSys.gameObjects[i].animation);

            // we want to do some work before we update the canvas,
            // otherwise we could use Ticker.addListener(stage);
            createjs.Ticker.addListener(GameSys);
            createjs.Ticker.useRAF = true;
            // Best Framerate targeted (60 FPS)
            createjs.Ticker.setFPS(GameSys.fps);
        } //startGame


    function reset() {
        var world = GameSys.gameContainer;
        var hero = GameSys.hero;
        var go = GameSys.gameObjects;

        world.removeAllChildren();
        world.x = world.y = 0;

        hero.x = 50;
        hero.y = 50;

        for (var i = 0; i < go.length; i++) {
            world.addChild(go[i].animation);
        }
        world.addChild(hero.animation);
    }


    GameSys.tick = function() {
        GameSys.stage.update();


        var hero = GameSys.hero;
        var world = GameSys.gameContainer;
        var w = GameSys.width;
        var h = GameSys.height;

        var wd = 0.5;
        var hd = 0.5;


        for (var i = 0; i < GameSys.bricks.length; i++) {
            var inter = calculateRectIntersection(hero,GameSys.bricks[i]);
            if (inter) break;
        }


        writeOnCanvas("getBounds=" + JSON.stringify(getBounds(hero)), 25, 25, "red");
        writeOnCanvas("getRectBounds=" + JSON.stringify(hero.getRectBounds()), 25, 50, "red");
        writeOnCanvas("inter=" + JSON.stringify(inter), 25, 75, "blue");

        adjustScrolling(hero, world, w, h, wd, hd);
        for (var i = 0; i < GameSys.gameObjects.length; i++) {
            GameSys.gameObjects[i].tick();
        }

    };
    //tick
    </script>

</body>

</html>
